import { Navigate } from 'react-router-dom';

type Role = 'student' | 'teacher' | 'admin';

export function RequireRole({ role, children }: { role: Role; children: JSX.Element }) {
  const token = localStorage.getItem('token');
  const currentRole = (localStorage.getItem('role') as Role | null) || 'student';
  
  // 如果没有token，跳转到登录页
  if (!token) return <Navigate to="/login" replace />;
  
  // 如果角色不匹配，跳转到首页
  if (currentRole !== role) return <Navigate to="/" replace />;
  
  return children;
}


